package com.itheima.ydd.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.R;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.YddAdvimgDto;
import com.itheima.ydd.entity.YddAdvimg;
import com.itheima.ydd.entity.YddAdvpos;
import com.itheima.ydd.service.IYddAdvimgService;
import com.itheima.ydd.service.IYddAdvposService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 广告图管理
 */
@RestController
@RequestMapping("/ydd_advimg")
public class YddAdvimgController {
    @Autowired
    private IYddAdvimgService yddAdvimgService;
    @Autowired
    private IYddAdvposService yddAdvposService;

    /**
     * 广告图详情
     *
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public ResultData rowInfo(Integer id) {
        YddAdvimg yddAdvimg = yddAdvimgService.getById(id);
        if (yddAdvimg != null) {
            return ResultData.ok("", yddAdvimg);
        }
        return ResultData.error("没有查询到对应信息");
    }

    /**
     * 广告图列表-分页
     *
     * @param advimgdesc
     * @param advimgpos
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/list")
    public ResultData list(String advimgdesc, String advimgpos, Integer page, Integer limit) {
        Page<YddAdvimg> pageInfo = new Page<>(page, limit);
        LambdaQueryWrapper<YddAdvimg> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotEmpty(advimgdesc), YddAdvimg::getAdvimgdesc, advimgdesc);
        lqw.like(StringUtils.isNotEmpty(advimgpos), YddAdvimg::getAdvimgpos, advimgpos);
        lqw.orderByDesc(YddAdvimg::getId);
        yddAdvimgService.page(pageInfo, lqw);

        Page<YddAdvimgDto> dtoPage = new Page<>();
        List<YddAdvimg> records = pageInfo.getRecords();
        List<YddAdvimgDto> yddAdvimgDtoList = new ArrayList<>();
        records.forEach(yddAdvimg -> {
            YddAdvimgDto yddAdvimgDto = new YddAdvimgDto();
            LambdaUpdateWrapper<YddAdvpos> yddAdvposLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            yddAdvposLambdaUpdateWrapper.eq(yddAdvimg.getAdvimgpos() != null, YddAdvpos::getId, yddAdvimg.getAdvimgpos());
            YddAdvpos yddAdvpos = yddAdvposService.getOne(yddAdvposLambdaUpdateWrapper);
            BeanUtils.copyProperties(yddAdvimg, yddAdvimgDto);
            if (yddAdvpos != null) {
                yddAdvimgDto.setAdvposname(yddAdvpos.getAdvposname());
            }
            yddAdvimgDtoList.add(yddAdvimgDto);
        });
        dtoPage.setRecords(yddAdvimgDtoList);

        HashMap<String, Object> map = new HashMap<>();
        map.put("count", (int) pageInfo.getTotal());  //总数
        map.put("totalPages", (int) pageInfo.getPages()); //总页数
        map.put("pageSize", (int) pageInfo.getSize()); //每页数量
        map.put("currentPage", (int) pageInfo.getCurrent()); //每页页码
        map.put("data", dtoPage.getRecords());

        return ResultData.ok("", map);
    }

    /**
     * 广告图添加
     *
     * @param yddAdvimg
     * @return
     */
    @PostMapping("/add")
    public R add(@RequestBody YddAdvimg yddAdvimg) {
        boolean save = yddAdvimgService.save(yddAdvimg);
        if (save) {
            return R.success("", "广告图添加成功");
        }
        return R.error("添加失败");
    }

    /**
     * 广告位删除
     *
     * @param id
     * @return
     */
    @GetMapping("/del")
    public R delete(Integer id) {
        YddAdvimg yddAdvimg = yddAdvimgService.getById(id);
        if (yddAdvimg.getDefaultData() != null) {
            return R.error("当前是系统数据,不能删除!");
        }
        yddAdvimgService.removeById(id);
        return R.success("", "广告图删除成功");
    }

    /**
     * 广告图修改
     *
     * @param yddAdvimg
     * @return
     */
    @PostMapping("/update")
    public R update(@RequestBody YddAdvimg yddAdvimg) {
        boolean update = yddAdvimgService.updateById(yddAdvimg);
        if (update) {
            return R.success("", "修改成功");
        }
        return R.error("修改失败");
    }
}
